local skill = fk.CreateSkill{
    name = "#wk_heg__fenghuotu",
    tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["#wk_heg__fenghuotu"] = "缘江烽火图",

  ["#wk_heg__fenghuotu-choose"] = "缘江烽火图：可选择%arg个技能",
}

local H = require "packages/ol_hegemony/util"

--回合开始拿技能
skill:addEffect(fk.EventPhaseStart,{
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return H.compareKingdomWith(player, target) and player:hasSkill(skill.name) and #player:getPile("lord_fenghuo") > 0 and target.phase == Player.Start
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local skills = {"ld__lordsunquan_yingzi", "ld__lordsunquan_haoshi", "ld__lordsunquan_shelie", "ld__lordsunquan_duoshi"}
    local num = #player:getPile("lord_fenghuo") >= 5 and 2 or 1
    player:broadcastSkillInvoke("jiahe",math.random(3, 4))
    local result = room:askToCustomDialog(target,{
      skill_name = skill.name,
      qml_path = "packages/utility/qml/ChooseSkillBox.qml",
      extra_data = {
          table.slice(skills, 1, #player:getPile("lord_fenghuo") + 1),
          0,
          num,
          "#wk_heg__fenghuotu-choose:::" .. tostring(num),
      },})
    if result == "" then return false end
    local choice = result
    if #choice > 0 then
      room:handleAddLoseSkills(target, table.concat(choice, "|"), nil, true, false)
      room.logic:getCurrentEvent():findParent(GameEvent.Turn):addCleaner(function()
      room:handleAddLoseSkills(target, '-' .. table.concat(choice, "|-"), nil, true, false)
      end)
    end
  end,
})

--掉血丢烽火
skill:addEffect(fk.Damaged,{
  anim_type = "negative",
  can_trigger = function (self, event, target, player, data)
    local type_remove = data.card and (data.card.type == Card.TypeTrick or data.card.trueName == "slash")
    if table.find(player.room.alive_players, function (p) return p:getMark("@@wk_heg__huanglong_change") ~= 0 end) then
      type_remove = data.card and (data.card.trueName == "slash")
    end
    return target == player and player:hasSkill(skill.name) and #player:getPile("lord_fenghuo") > 0 and type_remove
  end,
  on_cost = function (self, event, target, player, data)
    local card = player.room:askToCards(player,{
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = skill.name,
      pattern = ".|.|.|lord_fenghuo",
      prompt = "#ld__jiahe_damaged",
      expand_pile = "lord_fenghuo",
      cancelable = false,
    })
    if #card > 0 then
      event:setCostData(self, { card = card })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("jiahe", 5)
    room:moveCardTo(event:getCostData(self).card[1], Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skill.name, "lord_fenghuo", true, player)
  end,
})

local fenghuotu_attach = function (player)
    local room = player.room
    local players = room.alive_players
    local lordsunquans = table.filter(players, function(p) return p:hasShownSkill(skill.name) end)
    local jiahe_map = {}
    for _, p in ipairs(players) do
      local will_attach = false
      for _, ld in ipairs(lordsunquans) do
        if H.compareKingdomWith(ld, p) then
          will_attach = true
          break
        end
      end
      jiahe_map[p] = will_attach
    end
    for p, v in pairs(jiahe_map) do
      if v ~= p:hasSkill("wk_heg__jiahe_other&") then
        room:handleAddLoseSkills(p, v and "wk_heg__jiahe_other&" or "-wk_heg__jiahe_other&", nil, false, true)
      end
    end
  end


skill:addAcquireEffect(function (self, player, is_start)
  fenghuotu_attach(player)
end)

skill:addLoseEffect(function (self, player, is_death)
  fenghuotu_attach(player)
end)

skill:addEffect(fk.GeneralRevealed, {
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    fenghuotu_attach(player)
  end,
})

skill:addEffect(fk.Deathed,{
    can_refresh = function (self, event, target, player, data)
       return player:hasSkill(skill.name, true, true) and target == player
    end,
    on_refresh = function (self, event, target, player, data)
      fenghuotu_attach(player)
    end,
})

return skill